<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class WithdrawController extends HomeController {
 	
	public function _initialize(){
		
 		parent::_initialize();
	}
	/**
	 * 获取提现金额
	 */
	public function getWithdrawMoney(){
		$open_id = session('openid');
		$withdraw_money = I('withdraw_money');
		$phone = I('phone');
		$weixin = I('weixin');
		
		if(!$weixin){
		    $this->ajaxReturn(['status'=>-2,'msg'=>'请填写微信号'],'JSONP');
		}
		if(!$phone || !checkMobile($phone)){
		    $this->ajaxReturn(['status'=>-3,'msg'=>'请填写正确的手机号码'],'JSONP');
		}
		if(empty($withdraw_money) || $withdraw_money < 0){
			$this->ajaxReturn(['status'=>-1,'msg'=>'请正确填写金额'],'JSONP');
		}
		//充值地址
		$member = M('Member')->where(array('openid'=>$open_id))->find();
		$final_money = intval($member['money'])- intval($withdraw_money);
		if(intval($final_money) < 0){
			$this->ajaxReturn(['status'=>-2,'msg'=>'您的余额不足'],'JSONP');
		}
		$data['open_id'] = $open_id;
		$data['withdraw_money'] = $withdraw_money;
		$data['phone'] = $phone;
		$data['weixin'] = $weixin;
		$data['add_time'] = time();
		$data['status'] = 0;
		M()->startTrans();
		$re[] = M('Withdraw')->add($data);
		$re[] = M('Member')->where(array('openid'=>$open_id))->setDec('money',$withdraw_money);
		$re[] = $this->addFinance(2,1,$withdraw_money,'提现扣除');
		if(in_array(false, $re)){
			M()->rollback();
			$this->ajaxReturn(['status'=>-3,'msg'=>'系统繁忙，请稍候再试'],'JSONP');
		}else{
			M()->commit();
			$this->ajaxReturn(['status'=>1,'msg'=>'提交成功'],'JSONP');
		}
	}
	/**
	 * 获取会员本人提现列表
	 */
	public function withdrawList(){
		//是否登录
		//列出提现订单
		$open_id = session('openid');
		$page = I('page')?I('page'):null;
		if(empty($page)){
			$page = 1;
		}
		$limit = 4;
		$pages = dealPage($page,$limit);
		$list = M('Withdraw')->where(array('open_id'=>$open_id))->limit($pages['begin'],$pages['over'])->select();
		$this->ajaxReturn($list,'JSONP');
	}
}